xquery version "0.9-ml"

(: $root is a string representing the root node from which the id will be checked and the node to be updated will be calculted 
$id is the id of the node to update
$node is the node to update from the the end of the provided root
$value is the value the nodes text will updated to

the entire query is put into a string and evaluated because when mixing standard query evaluations with those that had been evaluated from a string, an error was thrown stating that external nodes could not be updated

further, within the string, there is no guarantee that a node will have a value. Text() returns an error when evaluating agains an empty element. So when inserting values we need to construct the lowest level element repersentation of the value e.g. <tag>val</tag>
:)

let $arg_root := xdmp:get-request-field("root")
let $arg_id := xdmp:get-request-field("id")
let $arg_att := "dateModified"

(:these variables are for testing :)
let $root := "doc('wines.xml')/wines/wine"
let $id := "10084"
let $att := "dateModified"


let $s := concat("let $root := ",$arg_root," 
				 ",
				 
				 "for $r in $root 
				 ",

				 " where $r/@id=",$arg_id, "
				 ",

				 " return 
				 ",

				 " xdmp:node-replace($r/@", $arg_att, ", attribute dateModified { fn:current-dateTime() } )"

				  )
return
xdmp:eval($s),
<result>true</result>







